home *** CD-ROM | disk | FTP | other *** search
/ CICA 1995 September (Japanese) / CICA Shareware for Windows CD-ROM (Walnut Creek) (September 1995) (Japanese) (Disc 2).iso / disc2 / nt / source.exe / POSIX / TOUCH / TOUCH.C < prev   
Encoding:
C/C++ Source or Header  |  1993-06-23  |  6.1 KB  |  239 lines

  1. /*
  2.  * Copyright (c) 1988 Regents of the University of California.
  3.  * All rights reserved.
  4.  *
  5.  * Redistribution and use in source and binary forms, with or without
  6.  * modification, are permitted provided that the following conditions
  7.  * are met:
  8.  * 1. Redistributions of source code must retain the above copyright
  9.  *    notice, this list of conditions and the following disclaimer.
  10.  * 2. Redistributions in binary form must reproduce the above copyright
  11.  *    notice, this list of conditions and the following disclaimer in the
  12.  *    documentation and/or other materials provided with the distribution.
  13.  * 3. All advertising materials mentioning features or use of this software
  14.  *    must display the following acknowledgement:
  15.  *    This product includes software developed by the University of
  16.  *    California, Berkeley and its contributors.
  17.  * 4. Neither the name of the University nor the names of its contributors
  18.  *    may be used to endorse or promote products derived from this software
  19.  *    without specific prior written permission.
  20.  *
  21.  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  22.  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  23.  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  24.  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  25.  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  26.  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  27.  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  28.  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  29.  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  30.  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  31.  * SUCH DAMAGE.
  32.  */
  33.  
  34. #ifndef lint
  35. char copyright[] =
  36. "@(#) Copyright (c) 1988 Regents of the University of California.\n\
  37.  All rights reserved.\n";
  38. #endif /* not lint */
  39.  
  40. #ifndef lint
  41. static char sccsid[] = "@(#)touch.c    4.8 (Berkeley) 6/1/90";
  42. #endif /* not lint */
  43.  
  44. /*
  45.  * Attempt to set the modify date of a file to the current date.  If the
  46.  * file exists, read and write its first character.  If the file doesn't
  47.  * exist, create it, unless -c option prevents it.  If the file is read-only,
  48.  * -f forces chmod'ing and touch'ing.
  49.  */
  50. #include <sys/types.h>
  51. #ifdef _POSIX_SOURCE
  52. #include <stdlib.h>
  53. #include <fcntl.h>
  54. #include <unistd.h>
  55. #else
  56. #include <sys/file.h>
  57. #endif
  58. #include <sys/stat.h>
  59. #include <stdio.h>
  60. #if WIN_NT
  61. #include <sys/cdefs.h>
  62. #include <misc.h>
  63. #include <bsdlib.h>
  64.  
  65. extern int globulate __P((int, int, char **));
  66. extern void deglobulate __P((void));
  67. extern int globulated_argc;
  68. extern char **globulated_argv;
  69. pid_t ppid;
  70. int globulation;
  71. #endif
  72.  
  73. static int    dontcreate;    /* set if -c option */
  74. static int    force;        /* set if -f option */
  75.  
  76. int touch __P((char *));
  77. int readwrite __P((char *, off_t));
  78. void usage __P((void));
  79.  
  80. int
  81. #if __STDC__
  82. main (int argc, char **argv)
  83. #else
  84. main(argc, argv)
  85.     int argc;
  86.     char **argv;
  87. #endif
  88. {
  89.     extern int optind;
  90.     int ch, retval;
  91.  
  92.  
  93. #if WIN_NT
  94.     ppid = getppid();
  95.     if (ppid == (pid_t) 1) /* if parent is CMD.EXE */
  96.     {
  97.         globulation = globulate(1, argc, argv);
  98.         if (globulation == 0)
  99.         {
  100.             argc = globulated_argc;
  101.             argv = globulated_argv;
  102.         }
  103.     }
  104. #endif
  105.     dontcreate = force = retval = 0;
  106.     while ((ch = getopt(argc, argv, "cf")) != EOF)
  107.         switch((char)ch) {
  108.         case 'c':
  109.             dontcreate = 1;
  110.             break;
  111.         case 'f':
  112.             force = 1;
  113.             break;
  114.         case '?':
  115.         default:
  116.             usage();
  117.         }
  118.     if (!*(argv += optind))
  119.         usage();
  120.     do {
  121.         retval |= touch(*argv);
  122.     } while (*++argv);
  123. #if WIN_NT
  124.     if (ppid == (pid_t) 1 && globulation == 0)
  125.         deglobulate();
  126. #endif
  127.     return retval;
  128. }
  129.  
  130. int
  131. #if __STDC__
  132. touch (char *filename)
  133. #else
  134. touch(filename)
  135.     char *filename;
  136. #endif
  137. {
  138.     struct stat statbuffer;
  139.  
  140.     if (stat(filename, &statbuffer) == -1) {
  141.         if (!dontcreate)
  142.             return(readwrite(filename, 0L));
  143. /* 
  144.    Marked off for option 'c' to work according
  145.    to BSD man pages
  146. */
  147. #if 0
  148.         fprintf(stderr, "touch: %s: does not exist\n", filename);
  149. #endif
  150.         return(EXIT_FAILURE);
  151.     }
  152.     if ((statbuffer.st_mode & S_IFMT) != S_IFREG) {
  153.         fprintf(stderr, "touch: %s: can only touch regular files\n",
  154.             filename);
  155.         return(EXIT_FAILURE);
  156.     }
  157.     if (!access(filename, R_OK | W_OK)) {
  158.         return(readwrite(filename,statbuffer.st_size));
  159.     }
  160.  
  161.     if (force) {
  162.         int retval;
  163.  
  164.         if (chmod(filename, S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH)) {
  165.             fprintf(stderr, "touch: %s: couldn't chmod: ",
  166.                 filename);
  167.             perror((char *)NULL);
  168.             return(EXIT_FAILURE);
  169.         }
  170.         retval = readwrite(filename, statbuffer.st_size);
  171.         if (chmod(filename, statbuffer.st_mode)) {
  172.             fprintf(stderr, "touch: %s: couldn't chmod back: ",
  173.                 filename);
  174.             perror((char *)NULL);
  175.             return(EXIT_FAILURE);
  176.         }
  177.         return(retval);
  178.     }
  179.     fprintf(stderr, "touch: %s: cannot touch\n", filename);
  180.     return(EXIT_FAILURE);
  181. }
  182.  
  183. int
  184. #if __STDC__
  185. readwrite (char *filename, off_t size)
  186. #else
  187. readwrite(filename, size)
  188.     char *filename;
  189.     off_t size;
  190. #endif
  191. {
  192.     int filedescriptor;
  193.     char first;
  194. #if !_POSIX_SOURCE
  195.     off_t lseek();
  196. #endif
  197.  
  198.     if (size) {
  199.         filedescriptor = open(filename, O_RDWR, 0);
  200.         if (filedescriptor == -1) {
  201. error:            fprintf(stderr, "touch: %s: ", filename);
  202.             perror((char *)NULL);
  203.             return(EXIT_FAILURE);
  204.         }
  205.         if (read(filedescriptor, &first, 1) != 1)
  206.             goto error;
  207. #ifdef _POSIX_SOURCE
  208.         if (lseek(filedescriptor, (off_t) 0, SEEK_SET) == (off_t) -1)
  209. #else
  210.         if (lseek(filedescriptor, 0L, 0) == -1)
  211. #endif
  212.                             goto error;
  213.         if (write(filedescriptor, &first, 1) != 1)
  214.             goto error;
  215.     } else {
  216.         filedescriptor = creat(filename, 0666);
  217.         if (filedescriptor == -1)
  218.             goto error;
  219.     }
  220.     if (close(filedescriptor) == -1)
  221.         goto error;
  222.     return(EXIT_SUCCESS);
  223. }
  224.  
  225. void
  226. #if __STDC__
  227. usage (void)
  228. #else
  229. usage()
  230. #endif
  231. {
  232.     fprintf(stderr, "usage: touch [-cf] file ...\n");
  233. #if WIN_NT
  234.     if (ppid == (pid_t) 1 && globulation == 0)
  235.         deglobulate();
  236. #endif
  237.     exit(EXIT_FAILURE);
  238. }
  239.